package com.ajavaer.framework.web.controller;

import com.ajavaer.framework.core.web.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/")
public class ViewController extends BaseController {

    @RequestMapping("/")
    public ModelAndView index() {
        return new ModelAndView("index");
    }

    @GetMapping("/index")
    public ModelAndView viewsIndex() {
        return new ModelAndView("index");
    }

    @GetMapping("/layout")
    public ModelAndView viewsLayout() {
        return new ModelAndView("views/layout");
    }

    @GetMapping("/about")
    public ModelAndView viewsAbout() {
        return new ModelAndView("views/about");
    }

    @GetMapping("/theme")
    public ModelAndView viewsTheme() {
        return new ModelAndView("views/theme");
    }

    @GetMapping("/user/login")
    public ModelAndView viewsUserLogin() {
        return new ModelAndView("views/user/login");
    }

    @GetMapping("/403")
    public ModelAndView views403() {
        return new ModelAndView("views/error/403");
    }

    @GetMapping("/500")
    public ModelAndView views500() {
        return new ModelAndView("views/error/500");
    }

    @GetMapping("/404")
    public ModelAndView views404() {
        return new ModelAndView("views/error/404");
    }


    private static String extractPathFromPattern(
            final HttpServletRequest request) {
        String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
        String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
        return new AntPathMatcher().extractPathWithinPattern(bestMatchPattern, path);

    }

}
